package admin.controllers;

import helpers.IdUniqueHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import data.DealData;
import models.DealEmailModel;
import models.GeoModel;
import admin.data.DealEmailData;
import admin.data.GeoData;

@SuppressWarnings("serial")
public class AddDealEmailServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		HttpSession session = request.getSession();
		TreeMap<String, String> check = new TreeMap<String, String>();
		String city = (String) request.getParameter("city");
		
		DealEmailModel dealEmail = new DealEmailModel();
		
		GeoData geoData = new GeoData();
		ArrayList<GeoModel> cities = geoData.getCities();

		DealData dealData = new DealData();
		
		if (city != null) {
			dealData.getByCity(city);
		}
		
		request.setAttribute("cities", cities);
		request.setAttribute("city", city);
		request.setAttribute("deals", dealData.getListResult());
		request.setAttribute("check", check);
		request.setAttribute("dealEmail", dealEmail);
		
		try {
			request.getRequestDispatcher("/admin/pages/add_deal_email.jsp")
					.forward(request, response);
		} catch (ServletException e) {
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		HttpSession session = request.getSession();
		String city = (String) request.getParameter("city");

		DealData dealData = new DealData();
		DealEmailData deData = new DealEmailData();
		DealEmailModel dealEmail = new DealEmailModel();
		
		if (city == null) {
			response.sendRedirect("/admin/dealsemail/create");
			return;
		} else {
			dealData.getByCity(city);
		}

		String ids = (String) request.getParameter("ids");
		String title = (String) request.getParameter("title");
		
		TreeMap<String, String> check = new TreeMap<String, String>();
		
		GeoData geoData = new GeoData();
		ArrayList<GeoModel> cities = geoData.getCities();
		
		dealEmail.setCity(city);
		
		if (ids.isEmpty()) {
			check.put("ids", "Bạn chưa chọn bất cứ deal nào cả.");
			session.setAttribute("error", "Bạn chưa chọn bất cứ deal nào cả");
		}
		
		if (title.isEmpty()) {
			check.put("title", "Bạn chưa nhập tiêu đề cho mail");
		}
		
		if (check.size() == 0) {
			dealEmail.setTitle(title);
			dealEmail.setIds(ids);
			dealEmail.setId(IdUniqueHelper.getId());
			
			deData.insert(dealEmail);
			
			session.setAttribute("success", "Bạn đã tạo thành công Deal Email: " + dealEmail.getId());
			response.sendRedirect("/admin/dealsemail/list");
			return;
		} else {
			
		}
		
		request.setAttribute("cities", cities);
		request.setAttribute("city", city);
		request.setAttribute("deals", dealData.getListResult());
		request.setAttribute("check", check);
		request.setAttribute("dealEmail", dealEmail);
		try {
			request.getRequestDispatcher("/admin/pages/add_deal_email.jsp")
					.forward(request, response);
		} catch (ServletException e) {
		}
	}
}
